package org.dna.wickwtui.users;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.wicket.Component;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.dna.MetroApplication;
import org.dna.UserRoles;
import org.dna.metronomo.persistence.User;
import org.dna.uimodel.MenuItem;
import org.dna.uimodel.SelectableUser;
import org.dna.wickwtui.MetroPage;

/**
 * Page used to create a new user. Define user name, password and role.
 * */
@UserRoles( { "admin", "user" })
public class ManageUserPage extends MetroPage {
    private static final Logger LOG = Logger.getLogger(ManageUserPage.class);

	@UserRoles("admin")
	private class NewUserLink extends Link<Void> {

		public NewUserLink(String id) {
			super(id);
		}

		@Override
		public void onClick() {
			switchToPanel(UserDetailsPanel.class);
		}

		@Override
		public boolean isEnabled() {
            return m_currentPanel instanceof UserListPanel;
		}
	}

	@UserRoles( { "admin", "user" })
	private class ListUserLink extends Link<Void> {

		private static final long serialVersionUID = 1L;

		public ListUserLink(String id) {
			super(id);
		}

		@Override
		public void onClick() {
			switchToPanel(UserListPanel.class);
		}

		@Override
		public boolean isEnabled() {
            return m_currentPanel instanceof UserDetailsPanel;
		}
	}

	/**
	 * Boorkmarkable constructor
	 * */
	public ManageUserPage() {
		configurePanels();
		m_currentPanel = m_panelsMap.get(UserListPanel.class);
		add(m_currentPanel);
		m_pageContextMenu = preparePageContextMenu();
		updateContextMenu();
	}

	public static List<SelectableUser> allUsers() {
		List<User> users = MetroApplication.get().getUserDAO().users();
		List<SelectableUser> selectableUsers = new ArrayList<SelectableUser>(users.size());
		for (User user : users) {
			selectableUsers.add(new SelectableUser(user));
		}
		return selectableUsers;
	}

	private void configurePanels() {
		IModel<List<SelectableUser>> allUsersModel = new LoadableDetachableModel<List<SelectableUser>>() {

			@Override
			protected List<SelectableUser> load() {
				return allUsers();
			}
		}; 
		UserDetailsPanel userDetailsPanel = new UserDetailsPanel("innerPanel");
		UserListPanel userListPanel = new UserListPanel("innerPanel", allUsersModel);

		m_panelsMap.put(UserDetailsPanel.class, userDetailsPanel);
		m_panelsMap.put(UserListPanel.class, userListPanel);
	}

	private List<MenuItem> preparePageContextMenu() {
		MenuItem newUserItem = new MenuItem("New user", ManageUserPage.class) {
            @Override
			public Component createMenu() {
				NewUserLink link = new NewUserLink("link");
				link.add(new Label("caption", getCaption()));
				return link;
			}
		};

		MenuItem allUsersItem = new MenuItem("All user", ManageUserPage.class) {
            @Override
			public Component createMenu() {
				ListUserLink link = new ListUserLink("link");
				link.add(new Label("caption", getCaption()));
				return link;
			}
		};
		return Arrays.asList(newUserItem, allUsersItem);
	}
}
